package interfaz2;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This class is just used in the subscription option, when the "add
 * subscription" button is pressed. It is a dialog to ask the user for the
 * information needed to do the subscription.
 * 
 * Since it is just used inside the SubscriptionComposite module it may be
 * a private class inside that module, but it seems to me that the code needed
 * for this class is big enough to stay in a separate module, this way the
 * SubscriptionComposite code and this code itself are cleaner.
 * 
 * @author ole
 */
public class AddSubscriptionDialog extends DustDialog {
	private Text source_entry;
	private Text channel_entry;
	
	/*
	 * Private classes. Mostly classes to handle events of these dialogs.
	 */
	
	/**
	 * Class that will handle the selection of the subscription button of
	 * the dialog.
	 */
	private class SubscribeButtonListener extends SelectionAdapter {
		/**
		 * Get the URL typed in the text entry and dipose the dialog.
		 */
		@Override
		public void widgetSelected(SelectionEvent event) {
			String strings[] = new String[2];

			strings[0] = source_entry.getText();
			strings[1] = channel_entry.getText();
			result = strings;
			doExit();
		}
	}
	
	/**
	 * Class that will handle the selection of the cancel button.
	 */
	private class CancelButtonListener extends SelectionAdapter {
		/**
		 * Close the dialog.
		 */
		@Override
		public void widgetSelected(SelectionEvent event) {
			doExit();
		}
	}
	
	/*
	 * Public methods of the class AddSubscriptionDialog
	 */
	
	/**
	 * Class constructor.
	 */
	public AddSubscriptionDialog(Shell parent) {
		super(parent);
		
		GridData grid;
		Composite toplevel_comp, label_text_url_comp;
		Composite label_text_channel_comp, buttons_comp;
		Label label;
		Button subscribe_button, cancel_button;
		
		shell.setText("Add subscription");
		
		// We need a top-level composite to store at the top the label and the
		// text entry and at the bottom the buttons.
		toplevel_comp = new Composite(shell, SWT.BORDER);
		toplevel_comp.setLayout(new GridLayout());
		grid = new GridData(SWT.TOP, SWT.LEFT, true, true);
		toplevel_comp.setLayoutData(grid);
		
		// A second-level composite to store the label and the text entry for
		// the URL source.
		label_text_url_comp = new Composite(toplevel_comp, SWT.NONE);
		label_text_url_comp.setLayout(new GridLayout(2, false));
		grid = new GridData(SWT.LEFT, SWT.TOP, true, true);
		label_text_url_comp.setLayoutData(grid);
		
		// Another second-level composite to store the label and the text entry
		// for the channel name.
		label_text_channel_comp = new Composite(toplevel_comp, SWT.NONE);
		label_text_channel_comp.setLayout(new GridLayout(2, false));
		grid = new GridData(SWT.LEFT, SWT.TOP, true, true);
		label_text_channel_comp.setLayoutData(grid);
		
		// Another second-level composite to store the buttons.
		buttons_comp = new Composite(toplevel_comp, SWT.NONE);
		buttons_comp.setLayout(new GridLayout(2, true));
		grid = new GridData(SWT.RIGHT, SWT.TOP, true, true);
		buttons_comp.setLayoutData(grid);
		
		// The label for the source.
		label = new Label(label_text_url_comp, SWT.NONE);
		label.setText("Feed URL:");
		
		// The text entry for the source.
		this.source_entry = new Text(label_text_url_comp, SWT.BORDER);
		
		// The label for the channel.
		label = new Label(label_text_url_comp, SWT.NONE);
		label.setText("Channel name:");
		
		// The text entry for the channel.
		this.channel_entry = new Text(label_text_url_comp, SWT.BORDER);
		
		// Subscribe button.
		subscribe_button = new Button(buttons_comp, SWT.NONE);
		subscribe_button.setText("Subscribe");
		grid = new GridData(SWT.FILL, SWT.FILL, true, true);
		subscribe_button.setLayoutData(grid);
		subscribe_button.addSelectionListener(new SubscribeButtonListener());
		
		// Cancel button.
		cancel_button = new Button(buttons_comp, SWT.NONE);
		cancel_button.setText("Cancel");
		grid = new GridData(SWT.FILL, SWT.FILL, true, true);
		cancel_button.setLayoutData(grid);
		cancel_button.addSelectionListener(new CancelButtonListener());
	}
}
